package yang.itcode.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import yang.itcode.model.Result;

import static yang.itcode.constant.ErrorConstant.SYSTEM_EXCEPTION_CODE;
import static yang.itcode.constant.ErrorConstant.SYSTEM_EXCEPTION_MESSAGE;

@RestControllerAdvice
public class GlobalExceptionHandle {
    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException e) {
        return Result.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        return Result.error(SYSTEM_EXCEPTION_CODE, SYSTEM_EXCEPTION_MESSAGE + e.getMessage());
    }
}
